package learn.algorithm.utils;

import java.lang.reflect.Field;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomElementGenerator<T> {
    private Class<T> clazz;
    private Random random = new Random();

    private static final  int CHAR_NUMBER = 26;
    private static final char CHAR_START = 'a';
    private static final int INT_NUMBER = 10;

    public RandomElementGenerator(Class<T> clazz) {
        this.clazz = clazz;
    }

    public List<T> generateElements(int size) throws Exception {
        List<T> list = new ArrayList<T>();
        for (int i = 0; i < size; i++) {
            T element = clazz.getDeclaredConstructor().newInstance();
            initializeElement(element);
            list.add(element);
        }
        return list;
    }

    private void initializeElement(T element) throws Exception {
        for (Field field : element.getClass().getDeclaredFields()) {
            field.setAccessible(true);  // 设置字段可访问性
            Class<?> fieldType = field.getType();
            if (fieldType == int.class || fieldType == Integer.class) {
                field.set(element, random.nextInt(INT_NUMBER));
            } else if (fieldType == double.class || fieldType == Double.class) {
                field.set(element, random.nextDouble());
            } else if (fieldType == float.class || fieldType == Float.class) {
                field.set(element, random.nextFloat());
            } else if (fieldType == long.class || fieldType == Long.class) {
                field.set(element, random.nextLong());
            } else if (fieldType == boolean.class || fieldType == Boolean.class) {
                field.set(element, random.nextBoolean());
            } else if (fieldType == char.class || fieldType == Character.class) {
                field.set(element, (char) (random.nextInt(CHAR_NUMBER) + CHAR_START));
            } else if (fieldType == String.class) {
                field.set(element, generateRandomString());
            }
        }
    }
    private String generateRandomString() {
        int length = random.nextInt(INT_NUMBER) + 1;  // 生成1到10之间的随机长度
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            char c = (char) (random.nextInt(CHAR_NUMBER) + CHAR_START);  // 生成小写字母
            sb.append(c);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        try {
            RandomElementGenerator<TestClass> generator = new RandomElementGenerator<>(TestClass.class);
            List<TestClass> elements = generator.generateElements(5);
            for (TestClass element : elements) {
                System.out.println(element);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
class TestClass {
    private int a;
    private double b;
    private float c;
    private long d;
    private boolean e;
    private char f;
    private String g;

    public TestClass() {
    }

    @Override
    public String toString() {
        return "TestClass{" +
                "a=" + a +
                ", b=" + b +
                ", c=" + c +
                ", d=" + d +
                ", e=" + e +
                ", f=" + f +
                ", g='" + g + '\'' +
                '}';
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public double getB() {
        return b;
    }

    public void setB(double b) {
        this.b = b;
    }

    public float getC() {
        return c;
    }

    public void setC(float c) {
        this.c = c;
    }

    public long getD() {
        return d;
    }

    public void setD(long d) {
        this.d = d;
    }

    public boolean isE() {
        return e;
    }

    public void setE(boolean e) {
        this.e = e;
    }

    public char getF() {
        return f;
    }

    public void setF(char f) {
        this.f = f;
    }

    public String getG() {
        return g;
    }

    public void setG(String g) {
        this.g = g;
    }
}